package com.tudouji.project.system.service.impl;

import java.util.List;

import com.flow.service.pojo.flow.FileInfo;
import com.flow.service.utils.FileUtils;
import com.tudouji.project.document.domain.DocumentRecord;
import com.tudouji.project.document.mapper.DocumentRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.system.mapper.FileinfoMapper;
import com.tudouji.project.system.domain.Fileinfo;
import com.tudouji.project.system.service.IFileinfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 附件信息Service业务层处理
 * 
 * @author tudouji
 * @date 2021-05-12
 */
@Service
public class FileinfoServiceImpl implements IFileinfoService 
{
    @Autowired
    private FileinfoMapper fileinfoMapper;
    @Autowired
    private DocumentRecordMapper recordMapper;

    /**
     * 查询附件信息
     * 
     * @param guid 附件信息ID
     * @return 附件信息
     */
    @Override
    public Fileinfo selectFileinfoById(String guid)
    {
        return fileinfoMapper.selectFileinfoById(guid);
    }

    /**
     * 查询附件信息列表
     * 
     * @param fileinfo 附件信息
     * @return 附件信息
     */
    @Override
    public List<Fileinfo> selectFileinfoList(Fileinfo fileinfo)
    {
        return fileinfoMapper.selectFileinfoList(fileinfo);
    }

    /**
     * 新增附件信息
     * 
     * @param fileinfo 附件信息
     * @return 结果
     */
    @Override
    public int insertFileinfo(Fileinfo fileinfo)
    {
        return fileinfoMapper.insertFileinfo(fileinfo);
    }

    /**
     * 修改附件信息
     * 
     * @param fileinfo 附件信息
     * @return 结果
     */
    @Override
    public int updateFileinfo(Fileinfo fileinfo)
    {
        return fileinfoMapper.updateFileinfo(fileinfo);
    }

    /**
     * 批量删除附件信息
     * 
     * @param guids 需要删除的附件信息ID
     * @return 结果
     */
    @Override
    public int deleteFileinfoByIds(String[] guids)
    {
        return fileinfoMapper.deleteFileinfoByIds(guids);
    }

    /**
     * 删除附件信息信息
     * 
     * @param guid 附件信息ID
     * @return 结果
     */
    @Override
    public int deleteFileinfoById(String guid)
    {
        return fileinfoMapper.deleteFileinfoById(guid);
    }
    @Transactional
    public int deleteFileinfoById(Long id)
    {
        FileInfo file = fileinfoMapper.selectFileInfoById(id);
        FileUtils.deleteFile(file.getUrl());
        //判断file 是否关联 单证记录表 ，关联的 需要删除单证记录
        if(file.getFileType()!=null){
            //删除单证记录
            DocumentRecord record=new DocumentRecord();
            record.setDocmentId(file.getFileType().longValue());
            record.setOrderId(file.getLinkedId());
            recordMapper.deleteDocumentRecordByIdDefId(record);
        }
        return fileinfoMapper.deleteFileInfoById(id);
    }
}
